package controllers

import argonaut.Argonaut.jNull
import dao._
import implicits.Implicits._
import models.Tables._
import org.joda.time.DateTime
import play.api.libs.json.{JsNull, JsValue}
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.{FormTool, Tool}
import utils.Utils

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import argonaut._
import Argonaut._
import ArgonautShapeless._
import tool.Pojo.SSample

import java.io.File

/**
 * Created by Administrator on 2019/8/7
 */
class SampleController @Inject()(cc: ControllerComponents)(
  implicit val accountDao: AccountDao,
  implicit val wechatMemberDao: WechatMemberDao,
  implicit val sampleDao: SampleDao
) extends
  AbstractController(cc) {

  def add = Action.async { implicit request =>
    val data = FormTool.addSampleForm.bindFromRequest().get
    val row = SampleRow(data.id, data.images, new DateTime(), workflow = jEmptyArray)
    sampleDao.insertOrUpdate(row).map { x =>
      Ok(Json("valid" := true))
    }
  }

  def batchAdd = Action.async { implicit request =>
    val datas = request.body.asJson.get.as[List[JsValue]]
    val rows = datas.map { dataJson =>
      val data = FormTool.addSampleForm.bind(dataJson).get
      SampleRow(data.id, data.images, new DateTime(), workflow = jEmptyArray)
    }
    sampleDao.updates(rows).map { x =>
      Ok(Json("valid" := true))
    }
  }

  def all = Action.async { implicit request =>
    sampleDao.selectAll.map { x =>
      val array = x.map(_.asJson)
      Ok(array.asJson)
    }
  }

  def delete = Action.async { implicit request =>
    val data = FormTool.idStrForm.bindFromRequest().get
    sampleDao.deleteById(data.id).map { x =>
      Ok(Json("valid" := true))
    }
  }

  def getById = Action.async { implicit request =>
    val data = FormTool.idStrForm.bindFromRequest().get
    sampleDao.selectById(data.id).map { x =>
      Ok(x.asJson)
    }
  }

  def getByIdNoImage = Action.async { implicit request =>
    val data = FormTool.idStrForm.bindFromRequest().get
    sampleDao.selectById(data.id).map { x =>
      val finalSample = x.migrateTo[SSample]
      Ok.sendFile(new File(""))
      Ok(finalSample.asJson)
    }
  }

  def update = Action.async { implicit request =>
    val data = FormTool.addSampleForm.bindFromRequest().get
    sampleDao.selectById(data.id).flatMap { dbX =>
      val row = dbX.copy(images = data.images, updateTime = new DateTime())
      sampleDao.update(row).map { x =>
        Ok(Json("valid" := true))
      }
    }
  }

  def idCheck = Action.async { implicit request =>
    val data = FormTool.idStrForm.bindFromRequest.get
    sampleDao.idExist(data.id).map { b =>
      if (b) {
        Ok(Json("valid" := false))
      } else {
        Ok(Json("valid" := true))
      }
    }
  }

  def idsCheck = Action.async { implicit request =>
    val data = FormTool.idStrsForm.bindFromRequest.get
    sampleDao.existIds(data.ids).map { ids =>
      println(ids)
      Ok(Json("existIds" := ids))
    }
  }

  def updateWorkflow = Action.async(parse.multipartFormData) { implicit request =>
    val data = FormTool.workflowForm.bindFromRequest().get
    sampleDao.selectById(data.id).flatMap { dbX =>
      val sampleIdDir = new File(Tool.sampleDir, data.id)
      val appendixDir = new File(sampleIdDir, "0").createDirectoryWhenNoExist
      println(request.body.files)
      val files = Tool.filesMoveDir("files", appendixDir)
      val fileNames = files.map(_.getName)
      val sampleInfo = Tool.getData(data.sampleInfo)
      val workflow = sampleInfo.workflow.map { workflow =>
        Json(
          "name" := workflow.name,
          "status" := workflow.status,
          "folder" := "0",
          "fileNames" := fileNames
        )
      }
      val row = dbX.copy(updateTime = new DateTime(), progress = data.progress, workflow = workflow.asJson)
      sampleDao.update(row).map { x =>
        Ok(Json("valid" := true))
      }
    }
  }


}
